<?php

namespace App\Utils;

class ResponseUtil
{
    /**
     * @param null $var
     * @return \Illuminate\Http\JsonResponse
     */
    public static function success($var=null){
        return self::res(0,'操作成功',$var);
    }

    /**
     * @param string $msg
     * @param int $code
     * @return \Illuminate\Http\JsonResponse
     */
    public static function error($msg = "操作失败",$code=-1){
        return self::res($code,$msg);
    }

    /**
     * @param int $code
     * @param string $msg
     * @param null $data
     * @return \Illuminate\Http\JsonResponse
     */
    public static function res($code=0,$msg="",$data=null){
        return response()->json(['code'=>$code,'msg'=>$msg,'data'=>$data])->setEncodingOptions(JSON_UNESCAPED_UNICODE);

    }

    /**
     * @param $var
     * @return \Illuminate\Http\JsonResponse
     */
    public static function json($var){
        return response()->json($var)->setEncodingOptions(JSON_UNESCAPED_UNICODE);
    }

    /**
     * @param \Illuminate\Contracts\Pagination\LengthAwarePaginator $paginate
     * @return \Illuminate\Http\JsonResponse
     */
    public static function paginate($paginate){
        $res = [];
        $res['code'] = 0;
        $res['data'] = $paginate->items();
        $res['count'] = $paginate->total();
        return self::json($res);
    }

}
